<template>
    <div id="excelToolDiv" class="app-container">
        <el-tabs v-model="activeName" type="border-card" style="margin-bottom: 10px">
            <el-tab-pane label="系数顶点中间值法" name="ff1">
                <div style="margin-bottom: 10px">
                    <el-button type="primary" @click="countTbjg">计算</el-button>
                    <el-divider direction="vertical"></el-divider>
                    <el-button type="primary" @click="revertTbjg">重置</el-button>
                    <el-divider direction="vertical"></el-divider>
                    <el-input v-model="tbData.jsl" style="width: 170px">
                        <template #prepend>
                            <span>计算列(D1)</span>
                        </template>
                    </el-input>
                    <el-divider direction="vertical"></el-divider>
                    <el-input v-model="tbData.pcsx" style="width: 170px">
                        <template #prepend>
                            <span>偏差筛选</span>
                        </template>
                    </el-input>
                    <el-divider direction="vertical"></el-divider>
                    <el-input v-model="tbData.jfxs" style="width: 170px">
                        <template #prepend>
                            <span>减分系数(E)</span>
                        </template>
                    </el-input>
                    <el-divider direction="vertical"></el-divider>
                    <el-input v-model="tbData.ydxs" style="width: 170px">
                        <template #prepend>
                            <span>引导系数</span>
                        </template>
                    </el-input>
                    <el-divider direction="vertical"></el-divider>
                    评审基准价(D):{{ tbData.valD }}
                    <el-divider direction="vertical"></el-divider>
                    sum:{{ selectSum }}
                </div>
                <div v-fresize="155" class="hotTableBox">
                    <hotTablePlus
                        ref="hotTablePlusRef"
                        :column-list="hotColumn0"
                        :data-list="hotData0"
                        @selectSum="
                            (selectSum1) => {
                                selectSum = selectSum1;
                            }
                        "
                    ></hotTablePlus>
                </div>
            </el-tab-pane>
        </el-tabs>
    </div>
</template>

<script>
import HotTablePlus from '@/components/hotTablePlus/index.vue';
import _ from 'lodash';
const lie2 = {
    A: '0',
    B: '1',
    C: '2',
    D: '3',
    E: '4',
    F: '5',
    G: '6',
    H: '7',
    I: '8',
    J: '9',
    K: '10',
    L: '11',
    M: '12',
    N: '13',
    O: '14',
    P: '15',
    Q: '16',
};
export default {
    name: 'excelJs',
    components: { HotTablePlus },
    data() {
        return {
            activeName: 'ff1',
            tabact: 1,
            hotColumn0: [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}],
            hotData0: [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}],
            selectSum: '',
            tbData: { jsl: 'A', ydxs: 0.95, pcsx: 0.2, jfxs: 1, valD: '' },
        };
    },
    mounted() {},
    methods: {
        revertTbjg() {
            this.$refs.hotTablePlusRef.hotSetData([{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}]);
        },
        // 计算平均价格
        jsArrAvg(arrTmp, key, condFun) {
            let averageArr = [];
            for (let i = 0; i < arrTmp.length; i++) {
                if (condFun(arrTmp[i])) {
                    averageArr.push(arrTmp[i][key]);
                }
            }
            return _.mean(averageArr);
        },
        countTbjg() {
            console.log('this.$refs.hotTablePlusRef.getHotData():', this.$refs.hotTablePlusRef.getHotData());
            // 开始计算，
            let lieH = this.tbData.jsl && lie2[this.tbData.jsl.toUpperCase()];
            let htData = this.$refs.hotTablePlusRef.getHotData();
            let liehaoNumArr = [];
            Object.keys(htData[0]).forEach((o) => {
                if (_.isNumber(+o) && +o >= 0) {
                    liehaoNumArr.push(+o);
                }
            });
            console.log('liehaoNumArr', liehaoNumArr);
            liehaoNumArr.sort();
            let addLie = liehaoNumArr[liehaoNumArr.length - 1] + 1;
            let dataArr = [];
            let valPcsx = +this.tbData.pcsx;
            let valK = +this.tbData.ydxs;
            let valD = undefined;

            // 有效数据筛选处理
            for (let i = 0; i < htData.length; i++) {
                console.log('htData:', htData[i][lieH]);
                if (htData[i][lieH] && +htData[i][lieH]) {
                    dataArr.push(Object.assign(htData[i], { row: i, jss: +htData[i][lieH], __color: '', isYx: 1 }));
                }
            }
            if (dataArr.length <= 5) {
                valD = this.jsArrAvg(dataArr, 'jss', (o) => o.isYx === 1) * valK;
            } else if (dataArr.length > 5) {
                // 去掉第一个和最后一个值
                dataArr = _.sortBy(dataArr, 'jss');
                dataArr[0]['isYx'] = 2;
                dataArr[dataArr.length - 1]['isYx'] = 2;
                // 计算第一个平均值
                let average1 = this.jsArrAvg(dataArr, 'jss', (o) => o.isYx === 1) * valK;
                // 计算每个数据的算数平均数偏差
                _.forEach(dataArr, (o) => {
                    if (o.isYx === 1) {
                        o.sspjppc = Math.abs(o.jss - average1) / average1;
                        // 偏差大于 valPcsx 的过滤掉
                        if (o.sspjppc > valPcsx) {
                            o.isYx = 3;
                        }
                        o.sspjppc1 = o.sspjppc.toFixed(4);
                    }
                });
                // 有效数据再算平均值
                valD = this.jsArrAvg(dataArr, 'jss', (o) => o.isYx === 1) * valK;
            }

            this.tbData.valD = valD;
            // 计算得分
            for (let i = 0; i < dataArr.length; i++) {
                if (dataArr[i].isYx === 1) {
                    let valF = 0;
                    if (valD === dataArr[i].jss) {
                        valF = 100;
                    }
                    if (dataArr[i].jss > valD) {
                        valF = 100 - (Math.abs(dataArr[i].jss - valD) / valD) * 100 * +this.tbData.jfxs;
                    }
                    if (dataArr[i].jss < valD) {
                        valF = 100 - (Math.abs(dataArr[i].jss - valD) / valD) * 100 * (+this.tbData.jfxs / 2);
                    }
                    valF = valF.toFixed();
                    dataArr[i].valF = valF;
                }
            }
            dataArr = _.sortBy(dataArr, 'row');
            _.forEach(dataArr, (o) => {
                let jg = '';
                let jgDesc = '';
                if (o.isYx === 1) {
                    jg = o.valF;
                    jgDesc += `有效数据;算数平均偏差${o.sspjppc1}`;
                    o[addLie + 1] = jg;
                    o[addLie + 2] = jgDesc;
                }
                if (o.isYx === 2) {
                    jgDesc += `失效数据1`;
                    o['__color'] = 'red';
                    o[addLie + 2] = jgDesc;
                }
                if (o.isYx === 3) {
                    jgDesc += `失效数据2;算数平均偏差${o.sspjppc1}`;
                    o['__color'] = 'red';
                    o[addLie + 2] = jgDesc;
                }
            });
            this.$refs.hotTablePlusRef.hotSetData(dataArr);
        },
    },
};
</script>

<style scoped>
.hotTableBox {
    display: block;
    position: relative;
    overflow: hidden;
}
</style>
